<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Club_ctrl extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('club_model');
        $this->load->model('admin_model');
    }

    public function club_view() {
        $data = $this->club_model->menu();

        $data['clubs'] = $this->club_model->get_all_club();

        $this->load->view('club_view.php', $data);
    }

    public function add_club() {
        $data = $this->club_model->menu();
        /*         * $this->load->library('googlemaps');
          $config = array(
          'center' => 'Davao,Philippines',
          'zoom' => 10
          );

          $this->googlemaps->initialize($config);

          $data['map'] = $this->googlemaps->create_map(); */
        $data['users'] = $this->admin_model->get_all_user();
        $this->load->view('addclub.php', $data);
    }

    function insert_club() {

        $this->load->library('form_validation');
        $this->form_validation->set_rules('clubname', 'Club name', 'required|xss_clean');
        $this->form_validation->set_rules('address', 'Address', 'required|xss_clean');
        $this->form_validation->set_rules('dateFounded', 'Date founded', 'required|xss_clean');
        $this->form_validation->set_rules('mobile', 'Mobile', 'required|xss_clean');

        $this->form_validation->set_rules('telno', 'Tel No.', 'required|xss_clean');
        $this->form_validation->set_rules('skype', 'Skype name', 'required|xss_clean');
        //$this->form_validation->set_rules('website', 'Website', 'required|valid_email|xss_clean');
        //$this->form_validation->set_rules('clubmaster', 'Club master', 'required|xss_clean');
        //$this->form_validation->set_rules('fbpage', 'Facebook', 'required|xss_clean');

        if ($this->form_validation->run() == TRUE) {
            $image_path = realpath(APPPATH . '../ClubLogo');
            $config['upload_path'] = $image_path;

            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '3000';
            $config['max_width'] = '1024';
            $config['max_height'] = '768';
            $image = $this->input->post('clubLogo');
            $this->load->library('upload');
            $this->upload->initialize($config);

            if ($this->upload->do_upload('clubLogo')) {
                $upload_data = $this->upload->data();

                $file_name = $upload_data['file_name'];
                $clubs = array(
                    'clubName' => $this->input->post('clubname'),
                    'clubmID' => $this->input->post('clubmaster'),
                    'clubComID' => $this->input->post('coclubmaster'),
                    'history' => $this->input->post('history'),
                    'dateFounded' => $this->input->post('dateFounded'),
                    'mobile' => md5($this->input->post('mobile')),
                    'phone' => $this->input->post('telno'),
                    'skype' => $this->input->post('skype'),
                    'fbpage' => $this->input->post('fbpage'),
                    'address' => $this->input->post('address'),
                    'website' => $this->input->post('website'),
                    'photo' => $file_name
                );

                $id = $this->club_model->insert_club($clubs);

                /*                 * foreach ($this->input->post('myTable') as $ranks)
                  {
                  $user_rank = array(
                  'userID' => $id,
                  'rank' => $this->input->post('cell1'),
                  'examDate' => $this->input->post('cell2'),
                  'promotionType' => $this->input->post('cell3')

                  );
                  $this->admin_model->insert_rank($user_rank);

                  } */

                $this->view_profile($id);
            } else {
                $this->add_club();
            }
        } else {
            $this->add_club();
        }
    }

    public function upload_photo() {
        $config['upload_path'] = './ClubPhotos/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '100';
        $config['max_width'] = '1024';
        $config['max_height'] = '768';

        $this->load->library('upload', $config);
        $files = $_FILES;
        $cpt = count($_FILES['clubPhotos']['name']);

        for ($i = 0; $i < $cpt; $i++) {
            $_FILES['clubPhotos']['name'] = $files['clubPhotos']['name'][$i];
            $_FILES['clubPhotos']['type'] = $files['clubPhotos']['type'][$i];
            $_FILES['clubPhotos']['tmp_name'] = $files['clubPhotos']['tmp_name'][$i];
            $_FILES['clubPhotos']['error'] = $files['clubPhotos']['error'][$i];
            $_FILES['clubPhotos']['size'] = $files['clubPhotos']['size'][$i];
            if (!$this->upload->do_upload('clubPhotos')) {
                
            }
        }
    }

    function edit_club($user_id, $action) {
        /**
          $data = $this->admin_model->menu();
          $user = $this->admin_model->get_user($user_id);
          $data['roles'] = $this->admin_model->get_role();
          $this->load->library('form_validation');
          $this->form_validation->set_rules('firstname', 'Firstname', 'required|xss_clean');
          $this->form_validation->set_rules('middlename', 'Middlename', 'required|xss_clean');
          $this->form_validation->set_rules('lastname', 'Lastname', 'required|xss_clean');
          $this->form_validation->set_rules('username', 'Username', 'required|xss_clean');
          $this->form_validation->set_rules('email', 'Email', 'required|xss_clean');
          $this->form_validation->set_rules('password', 'Password', 'required|xss_clean');
          if (isset($_POST) && !empty($_POST)) {
          $data = array(
          'fname' => $this->input->post('firstname'),
          'mname' => $this->input->post('middlename'),
          'lname' => $this->input->post('lastname'),
          'uname' => $this->input->post('username'),
          'email' => $this->input->post('email'),
          'photo' => $this->input->post('image1'),
          'club' => $this->input->post('club'),
          'style' => $this->input->post('style'),
          'isactive' => $this->input->post(1),
          );
          $data['fname'] = $_POST['firstname'];
          if ($this->form_validation->run() == true) {
          $this->admin_model->update_user($user_id, $data);

          $this->view_profile($user_id);
          }
          }

          $data['user'] = $user;

         */
        $this->view_profile($user_id);
    }

    function view_profile($club_id) {

        $data = $this->club_model->menu();
        $club = $this->club_model->get_club($club_id);

        $data['club'] = $club;

        $this->load->view('addminviewclubprofile', $data);
    }

    public function logout() {
        $newdata = array(
            'uid' => '',
            'uname' => '',
            'password' => '',
            'logged_in' => FALSE,
        );
        $this->session->unset_userdata($newdata);
        $this->session->sess_destroy();
        $this->index();
    }

    public function club() {
        $this->load->library('table');
        $this->load->helper('html');
        $this->load->model('admin_model');

        $data = $this->admin_model->menu();

        $this->load->view("club_view.php", $data);
    }

}
